﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 逻辑删除保健信息命令处理
    /// </summary>
    public class IsDelFarmingHealthCommandHandlter : IRequestHandler<IsDelFarmingHealthCommand, ApiResult<int>>
    {
        /// <summary>
        /// 保健信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingHealthcareModel> farmingHealthRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingHealthRep">保健信息仓储</param>
        public IsDelFarmingHealthCommandHandlter(IBaseRepository<FarmingHealthcareModel> farmingHealthRep)
        {
            this.farmingHealthRep = farmingHealthRep;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>

        public async Task<ApiResult<int>> Handle(IsDelFarmingHealthCommand request, CancellationToken cancellationToken)
        {


            //根据获取要逻辑删除的保健信息
            var health=farmingHealthRep.GetAll().FirstOrDefault(x=>x.HealthcareId==request.HealthcareId);
            if(health==null)
            {
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    data = 0,
                    message = "没有此数据！！！"

                };
            }
            //修改逻辑删除标识
            health.IsDel=true;
            await farmingHealthRep.UpdateAsync(health);


            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "删除成功"

            };
        }
    }

}
